<?php
/**
 * Library Of Shared Code (LOSC)
 *
 * LICENSE
 *
 * This source file is subject to the New BSD License that is bundled
 * with this package in the file LICENSE.txt.
 *
 * @category   LOSC Framework
 * @package    Filter
 * @copyright  Copyright (c) 2008 Robin Skoglund (http://robinsk.net/)
 * @license    http://creativecommons.org/licenses/BSD/  New BSD License
 */

/**
 * Filter for converting wiki markup to HTML
 *
 * @category   LOSC Framework
 * @package    Filter
 * @copyright  Copyright (c) 2008 Robin Skoglund (http://robinsk.net/)
 * @license    http://creativecommons.org/licenses/BSD/  New BSD License
 */
class Losc_Filter_WikiToHtml implements Zend_Filter_Interface
{
    /**
     * Patterns to match
     *
     * @var array
     */
    private static $_match = array(
        '/\*(\S[^\*]+\S)\*/',                // bold
        '/_(\S[^_]+\S)_/',                   // italic
        '/\\[(https?[^\s\]]+)\]/',           // link without title
        '/\\[(https?[^\s\]]+)\s([^\]]+)\]/', // link with title
    
        '/(?:\r\n|\n\r|\r|\n)/',             // newline
        '/\s\s+/'                            // excessive whitespace
    );
    
    /**
     * Replace occurences with this
     *
     * @var array
     */
    private static $_replace = array(
        '<b>$1</b>',
        '<i>$1</i>',
        '<a href="$1">$1</a>',
        '<a href="$1">$2</a>',
    
        '<br />',
        ' '
    );
    
    /**
     * Filters wiki markup to HTML
     *
     * @param  string $value
     * @return string
     */
    public function filter($value)
    {
        return preg_replace(self::$_match, self::$_replace, htmlentities($value));
    }
}
